package article.action;


import article.entity.ArticleVO;
import article.service.ArticleService;
import blogData.entity.BlogDataVO;
import blogData.service.BlogDataService;
import category.entity.CategoryVO;
import category.service.CategoryService;
import com.opensymphony.xwork2.ActionSupport;
import generic.page.Pages;
import label.entity.LabelVO;
import label.service.LabelService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import util.ObtainOther;

import java.util.Date;
import java.util.List;

public class ArticleAction extends ActionSupport {

    private final static Log LOGER = LogFactory.getLog(ArticleAction.class);

    private CategoryVO categoryVO;

    private ArticleVO articleVO;

    private List<LabelVO> labelVOs;

    private List<CategoryVO> categoryVOs;

    private LabelVO labelVO;

    private LabelService labelService;

    private String message;

    private ArticleService articleService;

    private CategoryService categoryService;

    private BlogDataService blogDataService;

    private Integer categoryId;

    private Integer articleId;

    private Pages pages;

    private Integer[] labelIds;

    public Integer[] getLabelIds() {
        return labelIds;
    }

    public void setLabelIds(Integer[] labelIds) {
        this.labelIds = labelIds;
    }

    public List<CategoryVO> getCategoryVOs() {
        return categoryVOs;
    }

    public void setCategoryVOs(List<CategoryVO> categoryVOs) {
        this.categoryVOs = categoryVOs;
    }

    public LabelService getLabelService() {
        return labelService;
    }

    public void setLabelService(LabelService labelService) {
        this.labelService = labelService;
    }

    public List<LabelVO> getLabelVOs() {
        return labelVOs;
    }

    public void setLabelVOs(List<LabelVO> labelVOs) {
        this.labelVOs = labelVOs;
    }

    public LabelVO getLabelVO() {
        return labelVO;
    }

    public void setLabelVO(LabelVO labelVO) {
        this.labelVO = labelVO;
    }

    public Pages getPages() {
        return pages;
    }

    public void setPages(Pages pages) {
        this.pages = pages;
    }

    public Integer getArticleId() {
        return articleId;
    }

    public void setArticleId(Integer articleId) {
        this.articleId = articleId;
    }

    public Integer getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }

    public BlogDataService getBlogDataService() {
        return blogDataService;
    }

    public void setBlogDataService(BlogDataService blogDataService) {
        this.blogDataService = blogDataService;
    }

    public CategoryVO getCategoryVO() {
        return categoryVO;
    }

    public void setCategoryVO(CategoryVO categoryVO) {
        this.categoryVO = categoryVO;
    }

    public ArticleVO getArticleVO() {
        return articleVO;
    }

    public void setArticleVO(ArticleVO articleVO) {
        this.articleVO = articleVO;
    }

    public CategoryService getCategoryService() {
        return categoryService;
    }

    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    public ArticleService getArticleService() {
        return articleService;
    }

    public void setArticleService(ArticleService articleService) {
        this.articleService = articleService;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * 添加文章，同时添加分类和标签
     * 先添加分类，返回分类id在进行添加文章的操作
     *
     * @return
     */
    public String addArticle() {
        try {
            Integer blogDataId = ObtainOther.obtainId();
            BlogDataVO blogDataVO = blogDataService.findByBlogDataId(blogDataId);

            categoryVO = categoryService.addCategoryVO(blogDataVO, categoryVO);

            articleVO.setPublishTime(new Date());
            articleVO = articleService.addArticleVO(articleVO, blogDataVO, categoryVO);

            labelVOs = labelService.addLabelVO(blogDataVO, labelVOs, articleVO);

//            articleVO=articleService.addArticleVOAll(articleVO,blogDataVO,categoryVO,labelVOs);

            return SUCCESS;
        } catch (Exception e) {
            message = ObtainOther.obtainMessage("addArticleError");

            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

    /**
     * 在有分类的前提下添加文章，即分类id已知
     *
     * @return
     */
    public String addArticleByCategory() {
        try {

            Integer blogDataId = ObtainOther.obtainId();
            BlogDataVO blogDataVO = blogDataService.findByBlogDataId(blogDataId);

            categoryVO = categoryService.findCategoryVOById(categoryId);
            labelVOs=labelService.findLabelVOByIds(labelIds,labelVOs);

            articleVO.setPublishTime(new Date());
            articleVO = articleService.addArticleVOAll(articleVO, blogDataVO, categoryVO, labelVOs);

            return SUCCESS;
        } catch (Exception e) {
            message = ObtainOther.obtainMessage("addArticleError");

            LOGER.error(message + ":" + e);
            return ERROR;
        }
    }

    public String inputAddArticle() {
        try {
            Integer blogDataId = ObtainOther.obtainId();
            categoryVOs = categoryService.findAllCategoryByBlogData(blogDataId);
            if (categoryVOs.size() == 0) {
                message = ObtainOther.obtainMessage("pleaseInputCategory");
                return INPUT;
            } else {
                labelVOs = labelService.findAllLabelByBlogData(blogDataId);
                if (labelVOs.size() == 0) {
                    message = ObtainOther.obtainMessage("pleaseInputLabel");
                    return INPUT;
                }
                return SUCCESS;
            }
        } catch (Exception e) {
            message = ObtainOther.obtainMessage("addArticleError");

            LOGER.error(message + ":" + e);
            return ERROR;
        }
    }

    /**
     * 删除文章
     *
     * @return
     */
    public String deleteArticle() {
        try {
            articleService.deleteArticleById(articleId);
            message = ObtainOther.obtainMessage("deleteArticleSuccess");

            return SUCCESS;
        } catch (Exception e) {
            message = ObtainOther.obtainMessage("deleteArticleError");
            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

    /**
     * 更新文章
     *
     * @return
     */
    public String updateArticle() {
        try {
            articleVO = articleService.updateArticle(articleVO);
            categoryVO = categoryService.updateCategory(categoryVO);
            message = ObtainOther.obtainMessage("updateArticleSuccess");

            return SUCCESS;
        } catch (Exception e) {
            message = ObtainOther.obtainMessage("updateArticleError");

            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

    /**
     * 读取文章
     *
     * @return
     */
    public String loadArticle() {
        try {
            articleVO = articleService.findArticleVOById(articleId);
            return SUCCESS;
        } catch (Exception e) {
            message = ObtainOther.obtainMessage("loadArticleError");

            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }


    /**
     * 博客文章分页显示
     *
     * @return
     */
    public String pageOfArticleByBlogData() {
        try {
            Integer blogDataId = ObtainOther.obtainId();
            pages = articleService.queryPageByBlogData(pages, blogDataId);
            return SUCCESS;
        } catch (Exception e) {
            message = ObtainOther.obtainMessage("listArticleError");

            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

    /**
     * 分类文章分页显示
     *
     * @return
     */
    public String pageOfArticleByCategory() {
        try {
            pages = articleService.queryPageByCategory(pages, categoryId);
            return SUCCESS;
        } catch (Exception e) {
            message = ObtainOther.obtainMessage("listArticleError");

            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

}
